﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication2
{
    [Serializable]
    public class Project
    {
        [XmlArray, XmlArrayItem(typeof(Item))]
        public ArrayList Items = new ArrayList();
    }
    [Serializable]
    [XmlInclude(typeof(HeCo))]
    [XmlInclude(typeof(ReCo))]
    [XmlInclude(typeof(TeCo))]
    [XmlInclude(typeof(Folder))]
    [XmlRoot("Item")]
    public class Item
    {
        private static Random rand = new Random();
        public Item()
        {
            Id = DateTime.Now.ToString() + rand.Next().ToString();
        }
        [XmlAttribute]
        public string Id;
        [XmlAttribute]
        public string Name ;
    }
    public class Folder : Item
    {
        public Folder()
        {
            //Name = "Folder";
        }
        [XmlArray, XmlArrayItem(typeof(Item))]
        public ArrayList Items = new ArrayList();
    }
    public class HeCo : Item
    {
        public HeCo()
        {
            Name = "HeCo";
        }
    }
    public class ReCo : Item
    {
        public ReCo()
        {
            Name = "ReCo";
        }
    }
    public class TeCo : Item
    {
        public TeCo()
        {
            Name = "TeCo";
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Project prj = new Project();
            Folder A = new Folder() ;
            Folder B = new Folder() { Name = "B" };
            Folder C = new Folder() { Name = "C" };
            HeCo heco = new HeCo();
            ReCo reco = new ReCo();
            TeCo teco = new TeCo();

            prj.Items.Add(A);
            A.Items.Add(B);
            A.Items.Add(new Folder() { Name = "D" });
            A.Items.Add(teco);
            B.Items.Add(C);
            B.Items.Add(heco);
            B.Items.Add(reco);
            new XmlSerializer(typeof(Project)).Serialize(XmlWriter.Create(@"e:\test1.xml"), prj);
            
            // read xml
            Project prj2;
            XmlSerializer reader = new XmlSerializer(typeof(Project));
            FileStream hh = File.OpenRead(@"e:\test.xml");
            prj2 = reader.Deserialize(hh) as Project;
            hh.Close();
            Folder items = prj2.Items[0] as Folder;
            Console.WriteLine(items.Items[1].ToString());
            Console.WriteLine(items.Name);
            Console.WriteLine( prj2.Items[0].ToString());
        }
    }
}
